package com.zzzy.cont.controller.processcon;

import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zzzy.common.util.FileUtil;
import com.zzzy.common.vo.AjaxResult;
import com.zzzy.common.vo.PageResult;
import com.zzzy.cont.dao.entity.Contract;
import com.zzzy.cont.service.processcon.ProcessConService;
import com.zzzy.cont.vo.contract.process.ProcessConButtonRes;
import com.zzzy.cont.vo.contract.process.ProcessConPageReq;
import com.zzzy.cont.vo.pageoffice.OpenOfficeFileRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Tag(name = "办理合同-待办合同", description = "con204")
@Validated
@RestController
@RequestMapping("/con/process")
public class ProcessingConController {

    @Resource
    private ProcessConService processConService;

    @Operation(summary = "分页列表")
    @GetMapping(value = "/processingPage")
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<PageResult<Contract>> processingPage(@Valid @ParameterObject ProcessConPageReq queryReq) {
        PageResult<Contract> conPage = processConService.getProcessingConPage(queryReq);
        return AjaxResult.success(conPage);
    }

    @GetMapping("/getButton/{id}")
    @Operation(summary = "获取合同可操作按钮")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<ProcessConButtonRes> getButton(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        ProcessConButtonRes buttonRes = processConService.getProcessingButton(id);
        return AjaxResult.success(buttonRes);
    }

    @GetMapping(value = "/getComparePageOfficeFile/{id}")
    @Operation(summary = "版本比较")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult<OpenOfficeFileRes> getComparePageOfficeFile(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        String openFileUrl = "/con/processConFileHistory/getConFileHistoryStream/";
        OpenOfficeFileRes openOfficeFileRes = processConService.getComparePageOfficeResByConId(id, openFileUrl);
        return AjaxResult.success(openOfficeFileRes);
    }

    @PutMapping("/back/{id}")
    @Operation(summary = "退回合同(审核、签订、归档共用方法)")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:page')")
    public AjaxResult back(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        processConService.back(id);
        return AjaxResult.success();
    }

    @PostMapping(value = "/updateAuditFile")
    @Operation(summary = "保存审核中文件")
    @Parameter(name = "id", description = "合同ID", required = true)
    @Parameter(name = "revisionCount", description = "Word中的修订痕迹数量", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:audit')")
    public void updateAuditFile(@NotNull(message = "合同ID不能为空") @RequestParam("id") Long id, Integer revisionCount
            , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        revisionCount = revisionCount == null ? 0 : revisionCount;
        processConService.updateWord(id, fs.getFileBytes(), revisionCount);
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PostMapping(value = "/updateSignFile/{id}")
    @Operation(summary = "保存签订中文件")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:sign')")
    public void updateSignFile(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id , HttpServletRequest request, HttpServletResponse response) {
        FileSaver fs = new FileSaver(request, response);
        byte[] byteArray = FileUtil.toByteArray(fs.getFileStream());
        processConService.updatePdf(id, byteArray);
        fs.setCustomSaveResult("ok");
        fs.close();
    }

    @PutMapping("/audit/{id}")
    @Operation(summary = "审核通过")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:audit')")
    public AjaxResult audit(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        processConService.audit(id);
        return AjaxResult.success();
    }

    @PutMapping("/sign/{id}")
    @Operation(summary = "签订通过")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:sign')")
    public AjaxResult sign(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        processConService.sign(id);
        return AjaxResult.success();
    }

    @PutMapping("/fulfill/{id}")
    @Operation(summary = "确认履行")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:fulfill')")
    public AjaxResult fulfill(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        processConService.fulfill(id);
        return AjaxResult.success();
    }

    @PutMapping("/archive/{id}")
    @Operation(summary = "归档通过")
    @Parameter(name = "id", description = "合同ID", required = true)
    @PreAuthorize("@pvs.hasPermission('con:process:archive')")
    public AjaxResult archive(@NotNull(message = "合同ID不能为空") @PathVariable("id") Long id) {
        processConService.archive(id);
        return AjaxResult.success();
    }
}
